const app = getApp();

import QRCode from '../../utils/weapp-qrcode';


let animation = wx.createAnimation({
  duration: 4000, //转盘转动时间
  timingFunction: 'ease-out' //动画效果
})
Page({
  data: {
    openid: '',

    CUSTOMER_TEL: '',
    phone: '', //手机号
    rotate: 0, //度数
    turning: false, //是否点击,
    ewmImg: '',
    prize: '', //中奖概率
    count: '', //剩余次数
    prizeList: [
      2150, //0  ...
      2100, //1 ...
      2200, // 2...
      2020, //3 ...
      2330, //4...
      2290, // 5...

      2600, //6...
      2610, // 7...

    ],
    //向1006传递的数据
    USER_PRIZE_ID: '',
    txtlist: '', //翻滚的字幕
    flag: true, //设置节流
    modalHidden: true, //新的图片模态框
    erdeng: true,
    sandeng: true,
    sideng: true,
    xiexie: true, //新的图片模态框   
    liudneg: true,
    qidieng: true,
    POST_SENT_HOME: '',
    POST_SENT_HOME_PRIZE_LEVEL: '',
    POST_SENT_HOME_USER_PRIZE_ID: '',
    ajaj: '',
    erweik: true,
    topshuju: '',

    linkuang: true, //支付宝
    login_id: '', //支付宝账号
    login_idaa: '', //支付宝账号
    POST_SENT_HOME_ID: '',
  },



  showPopup() {
    this.setData({
      modalHidden: !this.data.modalHidden,
      earth: false,
    });
  },


  modalCancelxin: function () {

    this.setData({

      xiexie: true,

    })
    wx.showModal({
      title: '温馨提示',
      content: '请在网点经理处领取幸运奖',
      success: function (res) {
        if (res.confirm) { //这里是点击了确定以后   
          console.log('用户点击确定')
        } else { //这里是点击了取消以后
          console.log('用户点击取消')
        }
      }
    })
  },

  quedingxin: function () {

    this.setData({

      qidieng: true,

    })
    wx.showModal({
      title: '温馨提示',
      content: '请在网点经理处领取幸运奖',
      success: function (res) {
        if (res.confirm) { //这里是点击了确定以后   
          console.log('用户点击确定')
        } else { //这里是点击了取消以后
          console.log('用户点击取消')
        }
      }
    })
  },


  //自定义模态框的取消
  modalCancel: function () {

    this.setData({
      modalHidden: true,
      erdeng: true,
      sandeng: true,
      sideng: true,
      xiexie: true,
      liudneg: true,
      qidieng: true,

    })
    console.log(222);
  },

  //自定义模态框的确定
  queding: function () {

    this.setData({
      modalHidden: true,
      erdeng: true,
      sandeng: true,
      sideng: true,
      xiexie: true,
      liudneg: true,
      qidieng: true,

    })
    console.log(111);
  },


  start() {





    let that = this
    // 节流
    if (that.data.flag) {

      that.data.flag = false
      wx.request({
        url: app.globalData.interface+'/hanzhong/API_7103_UserPrizeDraw',
        method: 'POST',
        data: {
          POST_SENT_HOME_PHONE: this.data.POST_SENT_HOME_PHONE,
          POST_SENT_HOME_ID: this.data.POST_SENT_HOME_ID,
          KEY: 'xianjgjKey9999',
          OPEN_ID: this.data.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {

          console.log(res);

          if (res.data.repeat != "error") {
            wx.showToast({
              title: res.data.data,
              icon: 'none',
              duration: 1500
            })
          }
          console.log(res.data.prize);
          console.log(res.data.myPrize);
          console.log(res.data.myPrize.POST_SENT_HOME_PRIZE_LEVEL);
          that.setData({
            POST_SENT_HOME_PRIZE_NAME: res.data.myPrize.POST_SENT_HOME_PRIZE_NAME,
            POST_SENT_HOME_PHONE: res.data.data.POST_SENT_HOME_PHONE,
            POST_SENT_HOME_PRIZE_LEVEL: res.data.myPrize.POST_SENT_HOME_PRIZE_LEVEL,
            POST_SENT_HOME_USER_PRIZE_ID: res.data.myPrize.POST_SENT_HOME_PRIZE_ID,
            ajaj: 'POST_SENT_HOME_USER_PRIZE_ID=' + res.data.userPrize.POST_SENT_HOME_USER_PRIZE_ID + '&' + 'POST_SENT_HOME_PRIZE_LEVEL=' + res.data.myPrize.POST_SENT_HOME_PRIZE_LEVEL + '&' + 'POST_SENT_HOME_PHONE=' + res.data.data.POST_SENT_HOME_PHONE,
          })

          console.log(that.data.ajaj);
          setTimeout(() => {
            that.erweima()
            that.setData({
              erweik: false
            })
          }, 5000)

          const index_ = res.data.prize;
          that.rotate(index_)

        }
      })
      setTimeout(() => {
        that.data.flag = true
      }, 5500)
    }



    // const index_ = 6;
    // that.rotate(index_)
  },

  rotate: function (index_) {
    let _this = this;
    // let cat = 60;
    if (!this.data.turning) {
      let rdm = 0; //度数
      rdm = Math.floor(Math.random() * 3600); //最大10圈
      // rdm = 2100; //最大10圈
      //用索引的方法对应角度
      // rdm = this.data.prizeList[index_ - 1]; //直接确定转的度数 
      rdm = this.data.prizeList[index_]; //直接确定转的度数 
      // rdm =2200;//直接确定转的度数 
      animation.rotate(rdm).step();
      this.setData({
        rotate: animation.export(),
        turning: true
      });
      setTimeout(() => {
        this.setData({
          turning: false
        });
        let num = rdm % 360; //转了多少度
        //console.log(num);
        function showModal(str) {
          wx.showModal({
            title: '恭喜抽中',
            content: str,
            success: function () { //点击确定后还原到0度
              let animation = wx.createAnimation({
                duration: 100,
                timingFunction: 'ease-out'
              });
              animation.rotate(0).step();
              _this.setData({
                rotate: animation.export(),
                turning: false
              });
            },
            fail: function (res) {
              console.log(res)
            }
          })
        }









        if (rdm == 2150) {

          this.setData({
            modalHidden: false
          })
        } else if (rdm == 2100) {

          this.setData({
            erdeng: false
          })
        } else if (rdm == 2200) {

          this.setData({
            sandeng: false
          })
        } else if (rdm == 2020) {

          this.setData({
            sideng: false
          })
        } else if (rdm == 2330) {

          this.setData({
            xiexie: false
          })


        } else if (rdm == 2290) {

          this.setData({
            liudneg: false
          })
        } else if (rdm == 2600) {
          this.setData({
            qidieng: false
          })
        }


      }, 4000)
    }
  },





  erweima() {

    console.log(this.data.ajaj);
    new QRCode('myQrcode', {
      text: this.data.ajaj,
      // text:'aaaa',
      width: 141, //canvas 画布的宽
      height: 141, //canvas 画布的高
      padding: 0, // 生成二维码四周自动留边宽度，不传入默认为0
      margin: 0,
      correctLevel: QRCode.CorrectLevel.L, // 二维码可辨识度
      callback: (res) => {
        //工具回调数据
        // 接下来就可以直接调用微信小程序的api保存到本地或者将这张二维码直接画在海报上面去，看各自需求
        wx.hideLoading()
        console.log("生成二维码", res)
        //将图片路劲放入data中，显示在wxml的image标签上
        this.setData({
          ewmImg: res.path,
          // showEwmIndex: e.currentTarget.dataset.index
        })

      }
    })
  },

  quno() {
    this.setData({
      linkuang: true,
    })
  },


  // // 支付宝
  tjok() {

    let that = this
    // if (that.data.login_idaa != that.data.login_id) {
    //   wx.showToast({
    //     title: '两次账号输入不一致！',
    //     icon: 'error',
    //     duration: 1500,
    //   })
    // } else {





    that.setData({
      openid: app.globalData.openid
    })


    wx.request({
      url: app.globalData.interface+'/hanzhong/API_7105_sendTicketCouponStateALiPay',
      method: 'POST',
      data: {
        USER_PRIZE_ID: that.data.POST_SENT_HOME_USER_PRIZE_ID,
        // login_id: that.data.login_id,
        // openid: that.data.openid,
        USER_ASSETS_TICKET_PHONE: that.data.POST_SENT_HOME_PHONE,
        OPEN_ID: that.data.openid, //用户OPEN_ID
        session_key: that.data.session_key,
        OPEN_UUID: that.data.jiami,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        console.log(res.data);
        if (res.data.result == "success") {
          wx.showModal({
            title: '温馨提示',
            content: res.data.data,
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后   
                console.log('用户点击确定')
                that.setData({
                  linkuang: true,
                })
              } else { //这里是点击了取消以后
                console.log('用户点击取消')
                that.setData({
                  linkuang: true,
                })
              }
            }
          })

        } else {
          wx.showModal({
            title: '温馨提示',
            content: res.data.data,
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后   
                console.log('用户点击确定')
                that.setData({
                  linkuang: true,
                })
              } else { //这里是点击了取消以后
                console.log('用户点击取消')
                that.setData({
                  linkuang: true,
                })
              }
            }
          })
        }
      }
    })
    // }
  },








  onLoad: function (e) {
    console.log(e);
    let topshu = JSON.parse(e.data)
    console.log(topshu);
    console.log(topshu.byIdCard.POST_SENT_HOME_PHONE);
    console.log(topshu.byIdCard.POST_SENT_HOME_ID);

    if (topshu.userCoupon == 'null') {
      this.setData({
        POST_SENT_HOME_PHONE: topshu.byIdCard.POST_SENT_HOME_PHONE,
        POST_SENT_HOME_ID: topshu.byIdCard.POST_SENT_HOME_ID,
      })
    } else {
      console.log(topshu.userCoupon.POST_SENT_HOME_PHONE);
      this.setData({
        VALID: topshu.userCoupon.VALID,
        POST_SENT_HOME_PRIZE_NAME: topshu.userCoupon.POST_SENT_HOME_PRIZE_NAME,
        topshuju: topshu.userCoupon,
        POST_SENT_HOME_PHONE: topshu.userCoupon.POST_SENT_HOME_PHONE,
        POST_SENT_HOME: topshu.userCoupon.POST_SENT_HOME_PHONE,
        POST_SENT_HOME_PRIZE_LEVEL: topshu.userCoupon.POST_SENT_HOME_PRIZE_LEVEL,
        POST_SENT_HOME_USER_PRIZE_ID: topshu.userCoupon.POST_SENT_HOME_USER_PRIZE_ID,
        ajaj: 'POST_SENT_HOME_USER_PRIZE_ID=' + topshu.userCoupon.POST_SENT_HOME_USER_PRIZE_ID + '&' + 'POST_SENT_HOME_PRIZE_LEVEL=' + topshu.userCoupon.POST_SENT_HOME_PRIZE_LEVEL + '&' + 'POST_SENT_HOME_PHONE=' + topshu.userCoupon.POST_SENT_HOME_PHONE,
        erweik: false,

      })
      console.log(this.data.ajaj);
      if (topshu.userCoupon.VALID == 2) {
        this.setData({
          linkuang: false,
          erweik: true,
        })
      }
      if (topshu.userCoupon.VALID == 3) {
        this.setData({
          linkuang: true,
          erweik: true,
        })
        wx.showModal({
          title: '温馨提示',
          content: '奖品已领取',
          success: function (res) {
            if (res.confirm) { //这里是点击了确定以后   
              console.log('用户点击确定')
            } else { //这里是点击了取消以后
              console.log('用户点击取消')
            }
          }
        })
      }

    }

  },


  onShow() {
    this.erweima()
    this.setData({

      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,


    })

  },




})